package com.aijk.web.admin.controller.medical;

import com.aijk.commons.lang.utils.StringUtil;
import com.aijk.medical.api.MedRecordApi;
import com.aijk.medical.model.MedRecord;
import com.aijk.medical.model.qo.MedRecordQo;
import com.aijk.web.admin.RetMsg;
import com.aijk.web.admin.controller.BaseController;
import com.aijk.web.admin.entity.Constant;
import com.aijk.web.admin.utils.DataTableUtil;
import com.github.pagehelper.PageInfo;
import com.google.common.collect.Lists;
import com.lifesea.familydoctor.api.FdTeamApi;
import com.lifesea.familydoctor.model.FdTeam;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;

import static com.sun.tools.javac.jvm.ByteCodes.ret;
import static sun.net.ftp.FtpReplyCode.find;

@Controller
@RequestMapping("/medical/record")
public class MedRecordController extends BaseController{
    private static Logger logger = LoggerFactory.getLogger(MedRecordController.class);

    @Autowired
    private MedRecordApi medRecordApi;
    @Autowired
    private FdTeamApi teamApi;

    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public String list(Model model) {
        //默认获取当前团队服务记录
        HttpSession session = request.getSession();
        FdTeam teamInfo = (FdTeam) session.getAttribute(Constant.SESSION_TEAM);
        if (teamInfo != null) {
            model.addAttribute("teamId", teamInfo.getTeamId());
            model.addAttribute("teamName", teamInfo.getTeamName());
        }else{
            model.addAttribute("msg", "身份信息已失效，请重新登录！");
            return "/error/error_msg";
        }
        model.addAttribute("idcard",(String) session.getAttribute(Constant.SESSION_IDCARD));
        return "/medical/record/list";
    }

    @RequestMapping(value = "/list", method = RequestMethod.POST)
    @ResponseBody
    public RetMsg list(@RequestParam HashMap skey, @RequestParam(value = "draw", defaultValue = "1") Long draw) {
        Long iTotalRecords;
        DataTableUtil.FormatOptions(skey);
        //关键字检索
        if ("1".equals(skey.get("type"))) {
            skey.put("ename", skey.get("keyword"));
        }else if("2".equals(skey.get("type"))){
            skey.put("emobile", skey.get("keyword"));
        }
        MedRecordQo qo = DataTableUtil.fillQo(skey, MedRecordQo.class);
        PageInfo<MedRecord> pageInfo = medRecordApi.page(qo);
        iTotalRecords = pageInfo.getTotal();
        RetMsg retMsg = new RetMsg();
        retMsg.put("data", pageInfo.getList());
        retMsg.put("draw", draw);
        retMsg.put("recordsTotal", iTotalRecords);
        retMsg.put("recordsFiltered", iTotalRecords);
        return retMsg;
    }

    @RequestMapping(value = "detail", method = RequestMethod.GET)
    public String detail(@RequestParam(value = "id", required = false, defaultValue = "0") Long id, Model model) {
        MedRecord medRecord = medRecordApi.findOne(id);

        if(medRecord != null){
            //图片
            List<String> list = Lists.newArrayList();
            String images = medRecord.getImage();
            if (StringUtil.isNotBlank(images)) {
                String[] arr = images.split(",");
                for (String img : arr) {
                    if(!"null".equals(img) && StringUtils.isNotBlank(img))
                        list.add(img);
                }
            }
            //查询记录对应的“团队服务机构”
            FdTeam team = teamApi.find(medRecord.getTeamId());
            if(team != null){
                model.addAttribute("teamOrg", team.getHospitalName());
            }
            model.addAttribute("images", list);
        }

        model.addAttribute("medRecord", medRecord);
        return "/medical/record/detail";
    }

    @RequestMapping(value = "/list_ecn", method = RequestMethod.GET)
    public String listEcn(@RequestParam String ecn,@RequestParam Integer signId, Model model) {
        model.addAttribute("ecn", ecn);
        model.addAttribute("signId", signId);
        return "/medical/record/list_ecn";
    }

    @RequestMapping(value = "/list_ecn", method = RequestMethod.POST)
    @ResponseBody
    public RetMsg listEcn(@RequestParam HashMap skey, @RequestParam(value = "draw", defaultValue = "1") Long draw) {
        Long iTotalRecords;
        DataTableUtil.FormatOptions(skey);
        //关键字检索
        if ("1".equals(skey.get("type"))) {
            skey.put("ename", skey.get("keyword"));
        }else if("2".equals(skey.get("type"))){
            skey.put("emobile", skey.get("keyword"));
        }
        MedRecordQo qo = DataTableUtil.fillQo(skey, MedRecordQo.class);
        //获取体检对象记录
        if(qo.getEcn() == null || qo.getEcn() == 0L){
            return RetMsg.failed("ecn为空");
        }
        PageInfo<MedRecord> pageInfo = medRecordApi.page(qo);
        iTotalRecords = pageInfo.getTotal();
        RetMsg retMsg = new RetMsg();
        retMsg.put("data", pageInfo.getList());
        retMsg.put("draw", draw);
        retMsg.put("recordsTotal", iTotalRecords);
        retMsg.put("recordsFiltered", iTotalRecords);
        return retMsg;
    }

    @RequestMapping(value = "detail_ecn", method = RequestMethod.GET)
    public String edit(@RequestParam(value = "id", required = false, defaultValue = "0") Long id, Model model) {
        MedRecord medRecord = medRecordApi.findOne(id);

        if(medRecord != null){
            //图片
            List<String> list = Lists.newArrayList();
            String images = medRecord.getImage();
            if (StringUtil.isNotBlank(images)) {
                String[] arr = images.split(",");
                for (String img : arr) {
                    if(!"null".equals(img) && StringUtils.isNotBlank(img))
                        list.add(img);
                }
            }
            //查询记录对应的“团队服务机构”
            FdTeam team = teamApi.find(medRecord.getTeamId());
            if(team != null){
                model.addAttribute("teamOrg", team.getHospitalName());
            }
            model.addAttribute("images", list);
        }

        model.addAttribute("medRecord", medRecord);
        return "/medical/record/detail_ecn";
    }

}